package com.ssyt.action;

import java.util.List;

import org.apache.struts2.ServletActionContext;

import com.ssyt.common.utils.AjaxCheck;
import com.ssyt.common.utils.BaseAction;
import com.ssyt.common.utils.MailUtil;
import com.ssyt.common.vo.Position;
import com.ssyt.common.vo.User;
import com.ssyt.service.UserService;

public class UserAction extends BaseAction {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private UserService userservice = new UserService();
	private List<Position> list;
	private String checkCode;
	private User user;
	
	public String getCheckCode() {
		return checkCode;
	}
	public void setCheckCode(String checkCode) {
		this.checkCode = checkCode;
	}
	public List<Position> getList() {
		return list;
	}
	public void setList(List<Position> list) {
		this.list = list;
	}
	public User getUser() {
		return user;
	}
	public void setUser(User user) {
		this.user = user;
	}

	/*
	 * 登陆验证
	 */
	public String login() {
		String string = null;
		System.out.println(user);
		user = userservice.loginInfo(user);
		list = userservice.listLatestPostion();
		if (user == null) {
			getSession().put("user", user);
			getSession().put("msg", "用户名或者密码错误！");
			string = "error";
		} else {
			if (user.getIs_active() != 2) {
				getSession().put("msg", "该用户没有激活，请联系管理员！");
				string = "error";
			} else {
				getSession().put("user", user);
				string = "login";
			}
		}

		// ActionContext.getContext().getSession().put("user", user);
		return string;
	}

	/**
	 * 退出
	 */
	public String loginOut() {
		ServletActionContext.getRequest().getSession().invalidate();
		return "loginout";
	}

	/**
	 * 账号设置跳转
	 */
	public String infoBind() {

		return "infobind";
	}

	/**
	 * 账号设置执行
	 */
	public String updateBind() {
		Integer resultNum = userservice.updateUser(user);
		if (resultNum == null) {
			getSession().put("msg", "修改失败！");
			return infoBind();

		}
		return "gotoindex";
	}

	/**
	 * 修改密码
	 * 
	 * @return
	 */
	public String updatePass() {
		userservice.updatePassWord(user);
		return "gotoindex";
	}

	/**
	 * 最新数据准备 还欠最热数据准备未完成
	 * 
	 * @return
	 */
	public String positionList() {
		list = userservice.listLatestPostion();
		//
		return "positionList";
	}

	public String initPass() {

		return "initPass";
	}

	/**
	 * 验证用户密码
	 */
	public void checkpass() {
		String string = null;
		String usercheckpassword = user.getPassword();
		if (usercheckpassword.length() > 0 && usercheckpassword != null) {
			user = userservice.checkUser(user);
			if (usercheckpassword.equals(user.getPassword())) {
				string = "ok";
			} else {
				string = "与原始密码不一致！";
			}
		} else {
			string = "请输入原始密码";
		}
		AjaxCheck.ReturnHttp(string);
	}

	/**
	 * 更改密码验证码验证
	 */


	public void checkCode() {
		String string = null;
		String kaptchaCode = (String) getSession().get(
				com.google.code.kaptcha.Constants.KAPTCHA_SESSION_KEY);
		if (kaptchaCode.equals(getCheckCode())) {
			string = "ok";

		} else {
			string = "输入验证码有错";
		}
		AjaxCheck.ReturnHttp(string);
	}
	/**
	 * 验证邮箱是否可用
	 */
	public void  checkMail(){
		user=userservice.checkMail(user);
		if(user==null){
			AjaxCheck.ReturnHttp("ok");
		}else{
			AjaxCheck.ReturnHttp("该用户已经注册");
		}
	} 
     /*
      * 用户注册（激活）
      * 
      */
	public String register(){
		user.setIs_active(2);
		MailUtil.send(user.getEmail(), user);
		return "s";
	}
	/*
	 * 激活账号
	 * userActivation
	 */
	public String userActivation(){
		System.out.println(user+"激活完成");
		userservice.addUser(user);
		return "useractivation";
		
	}
}
